package app.modules.controller;

import app.modules.Vo.FormRequestVo;
import com.authine.cloudpivot.engine.domain.bizservice.BizDatabaseConnectionPool;
import com.authine.cloudpivot.engine.service.bizservice.BizDatabaseConnectionPoolService;
import com.authine.cloudpivot.engine.service.datasource.DataSourceService;
import com.authine.cloudpivot.engine.service.impl.datasource.TemplateDynamicMultiDataSource;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;

@RestController
@RequestMapping("/demos")
@CrossOrigin
public class DemosController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private BizDatabaseConnectionPoolService connectionPoolService;


    public void test1() throws SQLException {
        //1.获取Driver的实现类对象
        Driver driver = new com.mysql.cj.jdbc.Driver(); //接口实例化
        String url="jdbc:mysql://localhost:3306/atguigudb?useUnicode=true&characterEncoding=utf-8";

        //Properties:将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","root");
        Connection conn = driver.connect(url,info);
        System.out.println(conn);
    }
    @PostMapping("/demo")
    public String demo(){
        String url="01242314431eqrqrereq1324241w23432114321";

        String vactionSql="select * from i_attendancereview";
        List<Map<String, Object>> vacationMaps = jdbcTemplate.queryForList(vactionSql);
        System.out.println(vacationMaps);
        return url;
    }

    public void test5() throws Exception {
//    //1.读取配置文件中的4个基本信息
//    InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
//    Properties pros = new Properties();
//    pros.load(is);
//    String user = pros.getProperty("user");
//    String password = pros.getProperty("password");
//    String url = pros.getProperty("url");
//    String driverClass = pros.getProperty("driverClass");
//    //2.加载驱动
//    Class.forName(driverClass);
//    //3.获取连接
//    Connection connection = DriverManager.getConnection(url, user, password);
//    System.out.println(connection);

        //注册数据库驱动： 加载适当的数据库驱动程序，可以通过反射方式来加载。
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL数据库示例
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//建立数据库连接： 使用DriverManager获取数据库连接，提供数据库的URL、用户名和密码。
        String url = "jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8&useSSL=false"; // 数据库URL，“testdb” 这里改为你要操作的数据库名，其余部分不需要改
        String user = "root"; // 用户名，这个用户名一般默认是"root"
        String password = "your_password"; // 密码，这里填写你自己的MySQL密码

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }


}

    @PostMapping("/test")
    public String test(){
        String url="01242314431eqrqrereq1324241w23432114321";
        String vactionSql="select * from i_attendancereview";
        List<Map<String, Object>> vacationMaps = jdbcTemplate.queryForList(vactionSql);
        System.out.println(vacationMaps);
        return url;
    }

    @PostMapping("/insertOrder")
    public void insert(){
        BizDatabaseConnectionPool databaseConnectionPool = this.connectionPoolService.getByCode("demo");
        String datasourceType = databaseConnectionPool.getDatasourceType();
        String databaseType = databaseConnectionPool.getDatabaseType();
        String type = datasourceType.concat(".").concat(databaseType);
        DataSourceService dataSourceService = (DataSourceService) TemplateDynamicMultiDataSource.getDataSourceService("demo", type, JSON.toJSONString(databaseConnectionPool));
        StringBuilder sql = new StringBuilder("select top 10 * from T_BD_CUSTOMER");
        List<Object> sec = new ArrayList<>();
        //判断身份证号是否在hr系统黑名单中
        List<Map<String, Object>> list = dataSourceService.getJdbcTemplate().getJdbcTemplate().queryForList(sql.toString(),sec.toArray());
        System.out.println(list);
        String url="01242314431eqrqrereq1324241w23432114321";
    }

    @PostMapping("/insertkh")
    public void insertkh(@RequestBody Map<String,Object> pm){
        System.out.println("数据=="+pm);
        BizDatabaseConnectionPool databaseConnectionPool = this.connectionPoolService.getByCode("demo");
        String datasourceType = databaseConnectionPool.getDatasourceType();
        String databaseType = databaseConnectionPool.getDatabaseType();
        String type = datasourceType.concat(".").concat(databaseType);
        DataSourceService dataSourceService = (DataSourceService) TemplateDynamicMultiDataSource.getDataSourceService("demo", type, JSON.toJSONString(databaseConnectionPool));
        int number = (int)(Math.random()*10000);

        StringBuilder sql = new StringBuilder("insert into T_BD_CUSTOMER (FCUSTID,FMASTERID,FNUMBER,FUSEORGID) values(?,?,?,?)");
        List<Object> sec = new ArrayList<>();
       // int update = jdbcTemplate.update(sql,pm.get("FNUMBER"),pm.get("FNUMBER"),pm.get("FNUMBER"));
        int update = dataSourceService.getJdbcTemplate().getJdbcTemplate().update(sql.toString(),number,number,pm.get("FNUMBER"),pm.get("FUSEORGID"));
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
    }

    @PostMapping("/insertdd")
    public void insertdd(@RequestBody Map<String,Object> pm){
        System.out.println("数据=="+pm);
        BizDatabaseConnectionPool databaseConnectionPool = this.connectionPoolService.getByCode("demo");
        String datasourceType = databaseConnectionPool.getDatasourceType();
        String databaseType = databaseConnectionPool.getDatabaseType();
        String type = datasourceType.concat(".").concat(databaseType);
        DataSourceService dataSourceService = (DataSourceService) TemplateDynamicMultiDataSource.getDataSourceService("demo", type, JSON.toJSONString(databaseConnectionPool));
        int number = (int)(Math.random()*10000);

        StringBuilder sql = new StringBuilder("insert into T_BD_CUSTOMER (FCUSTID,FMASTERID,FNUMBER,FUSEORGID) values(?,?,?,?)");
        List<Object> sec = new ArrayList<>();
        // int update = jdbcTemplate.update(sql,pm.get("FNUMBER"),pm.get("FNUMBER"),pm.get("FNUMBER"));
        int update = dataSourceService.getJdbcTemplate().getJdbcTemplate().update(sql.toString(),number,number,pm.get("FNUMBER"),pm.get("FUSEORGID"));
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
    }

    @PostMapping("/insertbg")
    public void insertbg(@RequestBody Map<String,Object> pm){
        System.out.println("数据=="+pm);
        BizDatabaseConnectionPool databaseConnectionPool = this.connectionPoolService.getByCode("demo");
        String datasourceType = databaseConnectionPool.getDatasourceType();
        String databaseType = databaseConnectionPool.getDatabaseType();
        String type = datasourceType.concat(".").concat(databaseType);
        DataSourceService dataSourceService = (DataSourceService) TemplateDynamicMultiDataSource.getDataSourceService("demo", type, JSON.toJSONString(databaseConnectionPool));
        int number = (int)(Math.random()*10000);

        StringBuilder sql = new StringBuilder("insert into T_BD_CUSTOMER (FCUSTID,FMASTERID,FNUMBER,FUSEORGID) values(?,?,?,?)");
        List<Object> sec = new ArrayList<>();
        // int update = jdbcTemplate.update(sql,pm.get("FNUMBER"),pm.get("FNUMBER"),pm.get("FNUMBER"));
        int update = dataSourceService.getJdbcTemplate().getJdbcTemplate().update(sql.toString(),number,number,pm.get("FNUMBER"),pm.get("FUSEORGID"));
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
    }

    @PostMapping("/insertfh")
    public void insertfh(@RequestBody Map<String,Object> pm){
        System.out.println("数据=="+pm);
        BizDatabaseConnectionPool databaseConnectionPool = this.connectionPoolService.getByCode("demo");
        String datasourceType = databaseConnectionPool.getDatasourceType();
        String databaseType = databaseConnectionPool.getDatabaseType();
        String type = datasourceType.concat(".").concat(databaseType);
        DataSourceService dataSourceService = (DataSourceService) TemplateDynamicMultiDataSource.getDataSourceService("demo", type, JSON.toJSONString(databaseConnectionPool));
        int number = (int)(Math.random()*10000);

        StringBuilder sql = new StringBuilder("insert into T_BD_CUSTOMER (FCUSTID,FMASTERID,FNUMBER,FUSEORGID) values(?,?,?,?)");
        List<Object> sec = new ArrayList<>();
        // int update = jdbcTemplate.update(sql,pm.get("FNUMBER"),pm.get("FNUMBER"),pm.get("FNUMBER"));
        int update = dataSourceService.getJdbcTemplate().getJdbcTemplate().update(sql.toString(),number,number,pm.get("FNUMBER"),pm.get("FUSEORGID"));
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
    }

    @PostMapping("/insertth")
    public void insertth(@RequestBody Map<String,Object> pm){
        System.out.println("数据=="+pm);
        BizDatabaseConnectionPool databaseConnectionPool = this.connectionPoolService.getByCode("demo");
        String datasourceType = databaseConnectionPool.getDatasourceType();
        String databaseType = databaseConnectionPool.getDatabaseType();
        String type = datasourceType.concat(".").concat(databaseType);
        DataSourceService dataSourceService = (DataSourceService) TemplateDynamicMultiDataSource.getDataSourceService("demo", type, JSON.toJSONString(databaseConnectionPool));
        int number = (int)(Math.random()*10000);

        StringBuilder sql = new StringBuilder("insert into T_BD_CUSTOMER (FCUSTID,FMASTERID,FNUMBER,FUSEORGID) values(?,?,?,?)");
        List<Object> sec = new ArrayList<>();
        // int update = jdbcTemplate.update(sql,pm.get("FNUMBER"),pm.get("FNUMBER"),pm.get("FNUMBER"));
        int update = dataSourceService.getJdbcTemplate().getJdbcTemplate().update(sql.toString(),number,number,pm.get("FNUMBER"),pm.get("FUSEORGID"));
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
        System.out.println("是否更新"+update);
    }

}


